package com.yzt.sys.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.base.dto.ZTreeDTO;
import com.yzt.base.kit.Base;
import com.yzt.sys.entity.Permission;
import com.yzt.sys.kit.SysKit;
import com.yzt.sys.service.IPermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author Y
 * @since 2021-01-21
 */
@RestController
@RequestMapping("/sys/permission")
@Api(tags = "权限相关接口")
public class PermissionController {

	@Autowired
	private IPermissionService permissionService;

	@GetMapping("ztree")
	@ApiOperation("获取权限树")
	public Base<List<ZTreeDTO>> getZtree() {
		return Base.okData(permissionService.getZTree());
	}

	@PostMapping("")
	@ApiOperation("保存权限信息")
	public Base<Object> save(Permission permission) {
		boolean b = permissionService.save(permission);
		if (b) {
			return Base.ok();
		}
		return Base.fail();
	}

	@GetMapping("{id}")
	@ApiOperation("获取权限信息")
	public Base<Permission> get(@PathVariable("id") Long id) {
		Permission p = permissionService.getById(id);
		if (p != null) {
			Permission parent = permissionService.getById(p.getParentId());
			if (parent != null)
				p.setParentName(parent.getName());
			return Base.okData(p);
		}
		return Base.fail();
	}

	@PutMapping("")
	@ApiOperation("更新权限信息")
	public Base<Object> update(Permission p) {
		boolean b = permissionService.updateById(p);
		if (b)
			return Base.ok();
		return Base.fail();
	}

	@DeleteMapping("{id}")
	@ApiOperation("删除权限信息")
	public Base<Object> del(@PathVariable("id") Long id) {
		List<Permission> list = permissionService
				.list(Wrappers.<Permission>lambdaQuery().eq(Permission::getParentId, id));
		if (list.size() > 0) {
			return Base.fail("请先删除所有子节点");
		}
		boolean b = permissionService.removeById(id);
		if (b)
			return Base.ok();
		return Base.fail();
	}

	@GetMapping("list")
	@ApiOperation("权限列表")
	public Base<PageInfo<Permission>> list(SysKit kit) {
		PageInfo<Permission> info = PageHelper.startPage(kit.getPage(), kit.getLimit())
				.doSelectPageInfo(() -> permissionService.list(Wrappers.<Permission>lambdaQuery()
						.eq(StringUtils.isNotEmpty(kit.getName()), Permission::getName, kit.getName())));
		return Base.okData(info);
	}

}
